package KK.Loader
{
	//import KK.Config.LoaderConfig;
	import KK.GameBase.Events.SceneEvent;
	import KK.GameBase.IEventBase;
	import KK.GameBase.IMessageFollowers;
	import KK.GameBase.IMessageQueue;
	import KK.GameBase.MSGEvent;
	import KK.GameBase.SpriteBase;
	import KK.System.System;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	
	
	public final class MainLoading extends Sprite implements IMessageFollowers, IEventBase
	{
		static public const SHOW:String=String(0xFF1001);
		static public const HIDE:String=String(0xFF1002);
		static public const MAINLOADED_EVENT:String=String(0xFF1003);
		
		private var ps:MovieClip;
		private var _percent:int;
		private var _mainEvent:IMessageQueue;

		public function MainLoading()
		{
			super(); 
		}
		/**
		 * 绑定事件对象 
		 * @事件分发器 event
		 * 
		 */		
		public function BindEvent(_event:IMessageQueue):Boolean{
			this._mainEvent=_event;
			init();
			return true;
		}
		/**
		 * 初始化 
		 * 
		 */		
		private function init():void
		{ 
			
			Percent=0;
			 
			
			AttentionMessage(SHOW);
			AttentionMessage(HIDE);
			
			AttentionMessage(MProgressEvent.MProgress);
		}
		
		/**
		 * 设置并加载本类UI 
		 * @param url
		 * 
		 */		
		public function SetUIUrl(url:String):void{
			
			System.AddSwfLoader(url,Complete);
		}
		/**
		 *  当前MainLoading加载完成
		 * @param event
		 * 
		 */		
		private function Complete(event:Event):void
		{
			bind(event.target.content); 
			Publish(new SceneEvent(SceneEvent.ADD_SCENE_CHILD_EVENT,this,SpriteBase.TOP));
			
			Publish(new MSGEvent(Loading.HIDE));
			
			Publish(new MSGEvent(MAINLOADED_EVENT));
		}
		
		/**
		 * 设置进度百分比 
		 * @param value
		 * 
		 */
		public function set Percent(value:int):void
		{
			this._percent=value; 
			if(ps)
			{ 
				ps.percent.gotoAndStop(value);
			}
		}
		/**
		 * 设置加载信息
		 * @param value
		 * 
		 */
		public function setInfo(value:String):void
		{
			if(ps)
			{
				ps._loaderInfo.text=value;
			}
		}
		
		/**
		 *  获取进度
		 * @return 
		 * 
		 */
		public function get Percent():int
		{
			return _percent;
		}
		
		/**
		 *  设置加载进度信息
		 * @param value 加载信息
		 * @param percent 加载进度百分比
		 * 
		 */
		public function SetLoaderInfo(value:String,percent:int):void
		{
			setInfo(value);
			this.Percent=percent;
		}
		/**
		 * 绑定对象
		 * 
		 */		
		public function bind(mc:MovieClip):void
		{ 
			if(mc)
			{
				ps?removeChild(ps):null;
				ps=mc;
				addChild(ps);
				this.Percent=(this._percent);
			}
		} 
		
		/**
		 * 发布消息 
		 * @param msg
		 * 
		 */
		protected function Publish(msg:MSGEvent):void{
			if(_mainEvent)
			{
				_mainEvent.Publish(msg);
			}
		}
		/**
		 * 更新事件 
		 * @param event
		 * @return 
		 * 
		 */		
		public function Update(event:MSGEvent):void
		{
			switch(event.type)
			{
				case SHOW:
					this.visible=true;
					break;
				case HIDE:
					this.visible=false;
					Publish(new SceneEvent(SceneEvent.REMOVE_SCENE_CHILD_EVENT,this,SpriteBase.TOP));
					break;
				case MProgressEvent.MProgress:
					SetLoaderInfo(MProgressEvent(event).LoaderName,MProgressEvent(event).Percent);
					break;
			}
		}
		/**
		 * 事件监听 
		 * @param msg 监听消息
		 * @return 
		 * 
		 */
		public function AttentionMessage(msg:String):void
		{
			_mainEvent.AttentionMessage(msg,this);
		}
	}
}